package com.dai.zhenfujin.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.Constants.EventConstants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseFragment;
import com.dai.zhenfujin.bean.AlumFansListBean;
import com.dai.zhenfujin.bean.MessageEvent;
import com.dai.zhenfujin.bean.UserBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.my.activity.AnswerActivity;
import com.dai.zhenfujin.my.activity.AttestationActivity;
import com.dai.zhenfujin.my.activity.GeneralizeActivity;
import com.dai.zhenfujin.my.activity.MyActivity;
import com.dai.zhenfujin.my.activity.ReleaseActivity;
import com.dai.zhenfujin.my.activity.SettingActivity;
import com.dai.zhenfujin.my.activity.WalletActivity;
import com.dai.zhenfujin.utils.LogUtils;
import com.dai.zhenfujin.utils.SPUtils;
import com.dai.zhenfujin.utils.UIUtils;
import com.dai.zhenfujin.view.CommonDialog;
import com.dai.zhenfujin.visitingcard.app.ColleagueActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

public class MyFragment extends BaseFragment implements View.OnClickListener {
    private View view;
    private TextView tvName, tvCompany, tvPost;
    private ImageView ivSetup, ivMy;
    private RelativeLayout rlWallet, rlAttestation, rlMy, rlGeneralize,
            rlAnswer, rlRelease, rl_my, rlFans,rl_my_tongshi;
    private UserBean userBean;
    private FrameLayout fl_set;
    private Switch switch_xinjia;
    private String user_name;
    private String show_state;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public View initView() {
        LogUtils.d("initView");
        view = View.inflate(mContext, R.layout.fragment_my, null);

        ivSetup = view.findViewById(R.id.my_setup);
        ivMy = view.findViewById(R.id.my_imageview);
        tvName = view.findViewById(R.id.my_name);
        tvCompany = view.findViewById(R.id.my_company);
        tvPost = view.findViewById(R.id.my_post);
        rl_my = view.findViewById(R.id.rl_my);
        fl_set = view.findViewById(R.id.fl_set);
        switch_xinjia = view.findViewById(R.id.switch_xinjia);

        rlWallet = view.findViewById(R.id.my_layout_wallet);//钱包
        rlFans = view.findViewById(R.id.my_layout_fans);//粉丝福利
        rlAttestation = view.findViewById(R.id.my_layout_attestation);//认证
        rlMy = view.findViewById(R.id.my_layout_my);//我的发布
        rlRelease = view.findViewById(R.id.my_layout_release);//制作名片
        rlGeneralize = view.findViewById(R.id.my_layout_generalize);//推广赚钱
        rlAnswer = view.findViewById(R.id.my_layout_answer);//答题赚钱
        rl_my_tongshi = view.findViewById(R.id.my_tongshi);//答题赚钱

        ivSetup.setOnClickListener(this);
        rlWallet.setOnClickListener(this);
        rlAttestation.setOnClickListener(this);
        rlMy.setOnClickListener(this);
        rlGeneralize.setOnClickListener(this);
        rlAnswer.setOnClickListener(this);
        rlRelease.setOnClickListener(this);
        rl_my.setOnClickListener(this);
        rlFans.setOnClickListener(this);
        fl_set.setOnClickListener(this);
        rl_my_tongshi.setOnClickListener(this);
        return view;
    }

    private void openShowState(String show_state) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("show_state", show_state);
        params.put("req_bd", params2);
        HttpClient.post(mContext, Constants.UPDATESHOWSTATE, params, new CallBack<UserBean>() {
            @Override
            public void onSuccess(UserBean resultBean) {
                //  UIUtils.showToastSafe("设置成功");
            }
        });
    }

    @Override
    public void initData() {
        LogUtils.d("initView");
        super.initData();
        switch_xinjia.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (switch_xinjia.isChecked()){
                if(!switch_xinjia.isPressed())return;
                new CommonDialog(getActivity(), R.style.dialog, false, "该模式下您的产品及名片将不在展示", (dialog, confirm) -> {
                    if (confirm) { //true
                        openShowState("2");//打开
                    }else {
                        switch_xinjia.setChecked(false);
                    }
                    dialog.dismiss();
                }).show();
            }else {
                openShowState("1"); //关闭
            }
        });

        getUserInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event != null && event.getMessageKey() != null) {
            if (event.getMessageKey().equals(EventConstants.UPDATA_USER_PRODUCT)) {
                getUserInfo();
            }
        }
    }


    private void getUserInfo() {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("user_id", "");
        params.put("req_bd", params2);
        HttpClient.post(mContext, Constants.QUERYUSERINFO, params, new CallBack<UserBean>() {
            @Override
            public void onSuccess(UserBean resultBean) {
                userBean = resultBean;
                tvName.setText(resultBean.user_mobile);
                user_name = resultBean.getUser_name();
                tvCompany.setText(resultBean.getUser_name());
                tvPost.setText("邀请码:" + resultBean.invite_code);
                //  tvPost.setText(resultBean.getUser_duties());  //职位
                Glide.with(mContext).load(resultBean.getUser_logo()).into(ivMy);
                SPUtils.put(mContext, "user_logo", resultBean.getUser_logo());
                //头像
                SPUtils.saveConversionHeadPhoto(mContext, resultBean.user_id, resultBean.getUser_logo());
                //昵称
                SPUtils.saveConversionNickName(mContext, resultBean.user_id, resultBean.getUser_name());
                //职业
                SPUtils.saveConversionUserDuties(mContext, resultBean.user_id, resultBean.getUser_duties());
                //主业
                SPUtils.saveConversionJob(mContext, resultBean.user_id, resultBean.getUser_job());
                //user_id
                SPUtils.saveConversionUserId(mContext, resultBean.user_id, resultBean.user_id);
                //主业id
                SPUtils.put(mContext, Constants.TYPE_ID, resultBean.type_id);
                show_state = resultBean.show_state;
                SPUtils.put(mContext, Constants.USER_MOBILE, resultBean.user_mobile);
                switch_xinjia.setChecked(resultBean.show_state.equals("2"));
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_layout_fans://钱包
                Intent fans = new Intent(mContext, FansActivity2.class);
                startActivity(fans);
                break;
            case R.id.my_layout_wallet://钱包
                Intent wallet = new Intent(mContext, WalletActivity.class);
                startActivity(wallet);
                break;
            case R.id.my_layout_attestation://认证
                Intent attestation = new Intent(mContext, AttestationActivity.class);
                startActivity(attestation);
                break;
            case R.id.my_layout_my://我的主业和副业
                Intent my = new Intent(mContext, MyActivity.class);
                startActivity(my);
                break;
            case R.id.my_layout_release://我的名片
                Intent etting = new Intent(mContext, ReleaseActivity.class);
                etting.putExtra("userBean", userBean);
                startActivity(etting);
                break;

            case R.id.my_layout_generalize://推广赚钱
                Intent generalize = new Intent(getActivity(), GeneralizeActivity.class);
                generalize.putExtra("user_name", user_name);
                startActivity(generalize);
                break;
            case R.id.my_layout_answer://答题赚钱
                Intent answer = new Intent(mContext, AnswerActivity.class);
                startActivity(answer);
                break;
            case R.id.my_setup://设置
                Intent setup = new Intent(mContext, SettingActivity.class);
                startActivity(setup);
                break;
            case R.id.my_tongshi://同事
                Intent intent2 = new Intent(mContext, ColleagueActivity.class);
                if (null != userBean) {
                    intent2.putExtra("company_name", userBean.getUser_company());
                    intent2.putExtra("user_id", userBean.user_id);
                }
                startActivity(intent2);
                break;
            case R.id.rl_my://设置用户信息
                Intent intent = new Intent(mContext, MyInfoActivity.class);
                intent.putExtra("userBean", userBean);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
